package office.action.person;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;
import office.action.person.vo.OverTimeVo;
import office.bo.person.OverTime;
import office.service.person.IPersonService;
import platform.common.action.BaseAction;
import platform.common.exception.BusinessException;

@Namespace("/person")
@ParentPackage("struts-base")
@Controller("overTimeAction")
@Scope("prototype")
public class OverTimeAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OverTimeVo overTimeVo;
	
	private String flag;
	
	private List<OverTimeVo> overTimeList = new ArrayList<OverTimeVo>();
	
	private List<OverTimeVo> overTimeWaitList = new ArrayList<OverTimeVo>();
	
	private List<User> listUser = new ArrayList<User>();
	
	@Resource
	private IPersonService personalServiceImpl;
	
	@Resource
	private UserManagerService userManagerService;
	
	@Action(value="deleteOverTime")
	public String deleteOverTime(){
		personalServiceImpl.deleteOverTimeById(overTimeVo.getId());
		this.getAjaxMessagesJson().setMessage(true, "", "删除成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="gotoDeleteOverTime",results={@Result(location="/pages/person/workflow/overTimeDel.jsp")})
	public String gotoDeleteOverTime(){
		return "success";
	}
	
	@Action(value="gotoOverTime",results={@Result(location="/pages/person/workflow/overTimeEdit.jsp")})
	public String gotoOverTime(){
		listUser = userManagerService.getUserByRoleAndCompany("xzrsjl",getUserCompanyId());
		return "success";
	}
	
	@Action(value="gotoOverTimeMenu",results={@Result(location="/pages/person/workflow/overTimeXml.jsp")})
	public String gotoOverTimeMenu(){
		return "success";
	}
	
	@Action(value="saveOverTime")
	public String saveOverTime(){
		OverTime overTime = new OverTime();
		try {
			BeanUtils.copyProperties(overTime,overTimeVo);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		if(overTime != null && overTime.getId() != null && overTime.getId().equals("")){
			overTime.setId(null);
		}
		personalServiceImpl.saveOverTime(overTime);
		this.getAjaxMessagesJson().setMessage(true, "", "编辑成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="getOverTimeByUid",results={@Result(location="/pages/person/workflow/overTime.jsp")})
	public String getOverTimeByUid(){
		List<OverTime> list = personalServiceImpl.getOverTimeByUid(getUserId());
		for (Iterator<OverTime> iterator = list.iterator(); iterator.hasNext();) {
			OverTime overTime = (OverTime) iterator.next();
			OverTimeVo overTimeVo = new OverTimeVo();
			try {
				BeanUtils.copyProperties(overTimeVo, overTime);
				overTimeList.add(overTimeVo);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		if(getUserRoleName().equals("xzrsjl")){
			List<OverTime> plist = personalServiceImpl.getOverTimeByProcessUid(getUserId(),1);
			for (Iterator<OverTime> iterator = plist.iterator(); iterator.hasNext();) {
				OverTime overTime = (OverTime) iterator.next();
				OverTimeVo overTimeVo = new OverTimeVo();
				try {
					BeanUtils.copyProperties(overTimeVo, overTime);
					overTimeList.add(overTimeVo);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		return "success";
	}
	
	@Action(value="overTimeProcess",results={@Result(type="redirect",location="/person/getOverTimeByProcessUid?overTimeVo.processUser.id=${overTimeVo.processUser.id}&&overTimeVo.status=${overTimeVo.status}")})
	public String overTimeProcess(){
		try {
			OverTime overTime = personalServiceImpl.getOverTimeById(overTimeVo.getId());
			if(flag != null && !flag.equals("") && flag.equals("y")){
				overTime.setStatus(100);
			}else if(flag != null && flag.equals("n")){
				overTime.setStatus(0);
			}
			overTimeVo.setStatus(1);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	@Action(value="getOverTimeByProcessUid",results={@Result(location="/pages/person/workflow/overTime.jsp")})
	public String getOverTimeByProcessUid(){
		List<OverTime> list = personalServiceImpl.getOverTimeByProcessUid(getUserId(),overTimeVo.getStatus());
		for (Iterator<OverTime> iterator = list.iterator(); iterator.hasNext();) {
			OverTime overTime = (OverTime) iterator.next();
			OverTimeVo overTimeVo = new OverTimeVo();
			try {
				BeanUtils.copyProperties(overTimeVo, overTime);
				overTimeList.add(overTimeVo);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		return "success";
	}
	 
	public IPersonService getPersonalServiceImpl() {
		return personalServiceImpl;
	}

	public void setPersonalServiceImpl(IPersonService personalServiceImpl) {
		this.personalServiceImpl = personalServiceImpl;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public OverTimeVo getOverTimeVo() {
		return overTimeVo;
	}

	public void setOverTimeVo(OverTimeVo overTimeVo) {
		this.overTimeVo = overTimeVo;
	}
	public List<OverTimeVo> getOverTimeList() {
		return overTimeList;
	}
	public void setOverTimeList(List<OverTimeVo> overTimeList) {
		this.overTimeList = overTimeList;
	}

	public List<OverTimeVo> getOverTimeWaitList() {
		return overTimeWaitList;
	}

	public void setOverTimeWaitList(List<OverTimeVo> overTimeWaitList) {
		this.overTimeWaitList = overTimeWaitList;
	}

	public List<User> getListUser() {
		return listUser;
	}

	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}
}
